#include "devicelogdialog.h"
#include "ui_devicelogdialog.h"
#include "hmicommondefine.h"
#include "instructionEnum.h"


DeviceLogDialog::DeviceLogDialog(int styleId,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DeviceLogDialog)
{
    ui->setupUi(this);
    move(0,0);
    initialDisplayStyle(styleId);
//#ifdef BUILD_ARM
//    setGeometry(70,128,460,595);
//#endif

    startCalendar = new QCalendarWidget(this);
    endCalendar = new QCalendarWidget(this);

    startCalendar->hide();
    endCalendar->hide();

    QFont font;
    // 设置字体大小
    font.setPointSize(11);
//    startCalendar->setGeometry(20, 100, 420, 200);
//    endCalendar->setGeometry(20, 130, 420, 200);
    startCalendar->setFont(font);
    endCalendar->setFont(font);
    startCalendar->setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);
    endCalendar->setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);
    startCalendar->setStyleSheet("background:rgb(207, 244, 255);");
    endCalendar->setStyleSheet("background:rgb(207, 244, 255);");

    // 设置表格禁止编辑
    ui->message_tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

    //颜色设置
    QToolButton *tools = startCalendar->findChild<QToolButton*>(QLatin1String("qt_calendar_yearbutton"));
    tools->setStyleSheet("color: rgb(0, 0, 0);");
    tools = startCalendar->findChild<QToolButton*>(QLatin1String("qt_calendar_monthbutton"));
    tools->setStyleSheet("color: rgb(0, 0, 0);");
    tools = endCalendar->findChild<QToolButton*>(QLatin1String("qt_calendar_yearbutton"));
    tools->setStyleSheet("color: rgb(0, 0, 0);");
    tools = endCalendar->findChild<QToolButton*>(QLatin1String("qt_calendar_monthbutton"));
    tools->setStyleSheet("color: rgb(0, 0, 0);");

    //获取时间用的一个结构体指针
    tm_messageTime = NULL;

    //消息等级图标缓存
    messageLevelPicture.resize(EM_MESSAGE_LEVEL_END);
    messageLevelPicture[EM_MESSAGE_LEVEL_ERROR] = "/agvwork/AgvHmi/baseBag/hmiProject/resources/DeviceLogRes/error.png";//0
    messageLevelPicture[EM_MESSAGE_LEVEL_WARN] = "/agvwork/AgvHmi/baseBag/hmiProject/resources/DeviceLogRes/warn.png";//1
    messageLevelPicture[EM_MESSAGE_LEVEL_NOTE] = "/agvwork/AgvHmi/baseBag/hmiProject/resources/DeviceLogRes/note.png";//2
    messageLevelPicture[EM_MESSAGE_LEVEL_WAIT] = "/agvwork/AgvHmi/baseBag/hmiProject/resources/DeviceLogRes/wait.png";//3
}

void DeviceLogDialog::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        this->setGeometry(0,0,600,450);
        ui->message_tableWidget->setGeometry(10,154,580,230);
        ui->returnBtn->setGeometry(500,400,80,40);
        break;
    default:
        break;
    }
}

void DeviceLogDialog::resizeView()
{
    QStringList tableHeadLable;
    tableHeadLable << tr("产生时间") << tr("所属机器人") << tr("级别") << tr("消息ID") << tr("消息内容") ;
    ui->message_tableWidget->setHorizontalHeaderLabels(tableHeadLable);
    ui->message_tableWidget->setColumnCount(5);
    ui->message_tableWidget->setRowCount(message.size());
    // 设置列宽
    ui->message_tableWidget->setColumnWidth(0, 100);
    ui->message_tableWidget->setColumnWidth(1, 100);
    ui->message_tableWidget->setColumnWidth(2, 50);
    ui->message_tableWidget->setColumnWidth(3, 50);
    ui->message_tableWidget->setColumnWidth(4, 300);

    int size = message.size();
    //int start = ui->message_tableWidget->rowCount();
    for(int i = 0; i < size; i++)
    {
        ui->message_tableWidget->setItem(i, 0, new QTableWidgetItem(""));
        ui->message_tableWidget->setItem(i, 1, new QTableWidgetItem(""));
        ui->message_tableWidget->setItem(i, 2, new QTableWidgetItem(""));
        ui->message_tableWidget->setItem(i, 3, new QTableWidgetItem(""));
        ui->message_tableWidget->setItem(i, 4, new QTableWidgetItem(""));
    }
}

void DeviceLogDialog::updateView()
{
//    clock_t start_t,finish;
//    start_t = clock();

    ui->start_dateEdit->setDate(startDate);
    ui->end_dateEdit->setDate(endDate);

    startCalendar->setSelectedDate(startDate);
    endCalendar->setSelectedDate(endDate);

    qDebug()<<"all message.size() ==== "<<message.size();
    selectMessage = message;//selectMessage.operator = (message);
    tmpMsgTime.clear();
    tmpMsgTime.resize(message.size());
    tmpMsgTime_day.resize(message.size());

//    finish=clock();
//    qDebug()<<"time:"<<(finish - start_t);

//    start_t = clock();
    //根据日期筛选
    QString start = startDate.toString("yyyy-MM-dd");   //起始时间
    QString end = endDate.toString("yyyy-MM-dd");       //结束时间
    int size = 0;
    for(int i = 0; i < selectMessage.size(); i++)
    {

        messageTime = selectMessage[i].time;//需要后期再改动
        #ifdef BUILD_ARM
        messageTime = messageTime + 28800;          //28800 8小时的秒数
        #endif
        tm_messageTime = localtime(&messageTime);//从tm结构体中可以取到年月日时分秒等值。
        char charMessageTime[50] = {0};
        char tmpCompareTime[50] = {0};
        strftime(charMessageTime,50,"%Y-%m-%d %H:%M:%S",tm_messageTime);     //这里输出的将是 p的时间值
        strftime(tmpCompareTime,50,"%Y-%m-%d",tm_messageTime);                //这里输出的将是 p的时间值

        //QString date = QString(QLatin1String(szTmp));
        //qDebug()<<" time select:"<<date;

        tmpMsgTime[i] = QString(QLatin1String(charMessageTime));  //获取时间
        tmpMsgTime_day[i] = QString(QLatin1String(tmpCompareTime));  //获取时间

        if(tmpMsgTime_day[i] > end || tmpMsgTime_day[i] < start)
        {
            selectMessage.erase(selectMessage.begin() + i);
            tmpMsgTime.erase(tmpMsgTime.begin() + i);

            i--;
        }
    }
//    finish=clock();
//    qDebug()<<"time:"<<(finish - start_t);

//    start_t = clock();
    //根据消息类型及所属机器人筛选
    for(int i = 0; i < selectMessage.size();i ++)
    {
        if((selectMessage[i].MessageLevel == EM_MESSAGE_LEVEL_ERROR && errorFlag == 0)
            || (selectMessage[i].MessageLevel == EM_MESSAGE_LEVEL_WARN && warnFlag == 0)
            || (selectMessage[i].MessageLevel == EM_MESSAGE_LEVEL_NOTE && noteFlag == 0)
            || (selectMessage[i].MessageLevel == EM_MESSAGE_LEVEL_WAIT && waitFlag == 0)
            || (0 == showType && selectMessage[i].robotId != currentRobotId))
        {
            selectMessage.erase(selectMessage.begin() + i);
            tmpMsgTime.erase(tmpMsgTime.begin() + i);

            i--;
        }
    }
//    finish=clock();
//    qDebug()<<"time:"<<(finish - start_t);

//    start_t = clock();
    //最终根据条件筛选后的信息
    qDebug()<<"selectMessage.size() "<<selectMessage.size();

    QStringList tableHeadLable;
    tableHeadLable << tr("产生时间") << tr("所属机器人") << tr("级别") << tr("消息ID") << tr("消息内容") ;
    ui->message_tableWidget->setHorizontalHeaderLabels(tableHeadLable);
    ui->message_tableWidget->verticalHeader()->hide();//隐藏
//    ui->message_tableWidget->setColumnCount(5);
//    ui->message_tableWidget->setRowCount(selectMessage.size());
//    ui->message_tableWidget->setColumnWidth(2, 50);
//    ui->message_tableWidget->setColumnWidth(3, 50);

    // 设置列宽
    ui->message_tableWidget->setColumnWidth(0, 100);
    ui->message_tableWidget->setColumnWidth(1, 100);
    ui->message_tableWidget->setColumnWidth(2, 50);
    ui->message_tableWidget->setColumnWidth(3, 50);
    ui->message_tableWidget->setColumnWidth(4, 240);

    ui->message_tableWidget->selectRow(0);

    //显示表格内容
    size = selectMessage.size();
    for(int i = 0; i < size; i++)
    {
        ui->message_tableWidget->setRowHidden(i,false);

        QString str = "";

        //时间
        if(i < tmpMsgTime.size())
        {
            str = tmpMsgTime[i];
        }
        else
        {
            str = "-";
        }
        ui->message_tableWidget->item(i,0)->setText(str);

        //获取机器人名称
        for(int j = 0; j < robotIdName.size(); j ++)
        {
            if(selectMessage[i].robotId == robotIdName[j].robotId)
            {
                robotName = robotIdName[j].robotName;
                break;
            }
            else
            {
                robotName = "-";
            }
        }
        ui->message_tableWidget->item(i,1)->setText(robotName);

        //信息等级
        if(selectMessage[i].MessageLevel < EM_MESSAGE_LEVEL_END)
        {
            ui->message_tableWidget->item(i,2)->setIcon(QIcon(messageLevelPicture[selectMessage[i].MessageLevel] ));
        }

        //消息编码
        str = QString::number(selectMessage[i].messageCode);
        ui->message_tableWidget->item(i,3)->setText(str);

        //消息内容
        str = QString::fromStdString(selectMessage[i].MessageInformation);
        ui->message_tableWidget->item(i,4)->setText(str);
    }
    for(int i = size;i<message.size();i++)
    {
        ui->message_tableWidget->setRowHidden(i,true);
    }

    // 自适应行高
    ui->message_tableWidget->resizeRowsToContents();
//    finish=clock();
//    qDebug()<<"time:"<<(finish - start_t);

    //控件的状态刷新
    if(1 == errorFlag)
    {
        ui->error_checkBox->setChecked(true);
    }
    if(1 == warnFlag)
    {
        ui->warn_checkBox->setChecked(true);
    }
   if(1 == noteFlag)
    {
        ui->note_checkBox->setChecked(true);
    }
    if(1 == waitFlag)
    {
        ui->wait_checkBox->setChecked(true);
    }

    if(0 == showType)
    {
        ui->currentRobot_rBtn->setChecked(true);
    }
    else if(1 == showType)
    {
        ui->allRobot_rBtn->setChecked(true);
    }
}

void DeviceLogDialog::changeLanguage()
{
    ui->retranslateUi(this);
}

void DeviceLogDialog::on_startCalendar_Btn_clicked()
{
    if(startCalendar->isHidden())
    {
        startCalendar->show();
        endCalendar->hide();
        connect(startCalendar, SIGNAL(selectionChanged()), this, SLOT(startDateChanged()));
        connect(startCalendar, SIGNAL(clicked(QDate)), startCalendar, SLOT(hide()));
    }
    else if(!startCalendar->isHidden())
    {
        startCalendar->hide();
    }
}

void DeviceLogDialog::on_endCalendar_Btn_clicked()
{
    if(endCalendar->isHidden())
    {
        endCalendar->show();
        startCalendar->hide();
        connect(endCalendar, SIGNAL(selectionChanged()), this, SLOT(endDateChanged()));
        connect(endCalendar, SIGNAL(clicked(QDate)), endCalendar, SLOT(hide()));
    }
    else if(!endCalendar->isHidden())
    {
        endCalendar->hide();
    }
}

void DeviceLogDialog::on_currentRobot_rBtn_clicked()
{
    emit showCurrentRobotMessage();
}

void DeviceLogDialog::on_allRobot_rBtn_clicked()
{
    emit showAllRobotMessage();
}

void DeviceLogDialog::on_error_checkBox_clicked()
{
    emit errorMessageShowOrHide(errorFlag);
}

void DeviceLogDialog::on_warn_checkBox_clicked()
{
    emit warnMessageShowOrHide(warnFlag);
}

void DeviceLogDialog::on_note_checkBox_clicked()
{
    emit noteMessageShowOrHide(noteFlag);
}

void DeviceLogDialog::on_wait_checkBox_clicked()
{
    emit waitMessageShowOrHide(waitFlag);
}

void DeviceLogDialog::startDateChanged()
{
    emit startDateChanged(startCalendar->selectedDate());
}

void DeviceLogDialog::endDateChanged()
{
    emit endDateChanged(endCalendar->selectedDate());
}

void DeviceLogDialog::on_start_dateEdit_dateChanged(const QDate &date)
{
    emit startDateChanged(date);
}

void DeviceLogDialog::on_end_dateEdit_dateChanged(const QDate &date)
{
    emit endDateChanged(date);
}

DeviceLogDialog::~DeviceLogDialog()
{
    delete ui;
}

void DeviceLogDialog::on_returnBtn_clicked()
{
    this->hide();
}
